package org.zoomdev.zoom.dao.impl;

import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.dao.Dao;
import org.zoomdev.zoom.dao.adapters.StatementAdapter;
import org.zoomdev.zoom.dao.driver.SqlDriver;
import org.zoomdev.zoom.dao.meta.TableMeta;

public abstract class AbstractEntityFactory {

    protected final Dao dao;


    protected AbstractEntityFactory(Dao dao) {
        this.dao = dao;
    }

    interface ContextHandler<CONTEXT> {
        void handle(AbstractEntityField field, CONTEXT context);
    }

    interface ValueCasterCreator<CONTEXT> extends ContextCreator<CONTEXT, ValueCaster> {

    }

    interface StatementAdapterCreator<CONTEXT> extends ContextCreator<CONTEXT, StatementAdapter> {

    }

    interface ContextCreator<CONTEXT, T> {
        T create(CONTEXT context);
    }

    public void clearCache() {

    }

    protected TableMeta getTableMeta(String tableName) {
        TableMeta meta = dao.getDbStructFactory().getTableMeta(tableName);
        return meta;
    }

    String getSelectColumnName(String selectColumnName) {
        SqlDriver driver = dao.getDriver();
        if (selectColumnName.contains(" ") || selectColumnName.contains("(")
                || selectColumnName.contains(".") || selectColumnName.contains("*")) {
            return selectColumnName;
        } else {
            return driver.protectColumn(selectColumnName);
        }
    }

}
